package Presenter;



import Model.ConfigScreenModel;

import Model.Player;
import Model.Spaceship.Gnat;
import View.ConfigScreenView;
import View.GUI;


public class ConfigScreenPresenter{
	/**
	 * 
	 */
	private final ConfigScreenModel model;

	/**
	 * 
	 */
	private String name;
	
	/**
	 * Instantiates the presenter for the game's configuration screen; sets the
	 * model to an instance of ConfigScreenModel()
	 */
	public ConfigScreenPresenter(){
		model = new ConfigScreenModel();
	}
	
	/**
	 * @param value the level of the player's pilot attribute
	 * @return pilotLevel the player's choice for pilot level
	 */
	public int changePilotLevel(int value){
		final int pilotLevel = model.getPilotLevel();
		final int totalPoints = model.getTotalPoints();
		
		if(value <= totalPoints && (value + pilotLevel) >= 0){
			//System.out.println("hitA: " + value + " totalPoints: " + totalPoints);
			model.setPilotLevel(pilotLevel + value);
			model.setTotalPoints(totalPoints - value);
			return pilotLevel + value;
		}
		
		return pilotLevel;
	}
	
	/**
	 * @param value the level of the player's trader attribute
	 * @return traderLevel the player's choice for trader level
	 */
	public int changeTraderLevel(int value){
		final int traderLevel = model.getTraderLevel();
		final int totalPoints = model.getTotalPoints();
		
		if(value <= totalPoints && (value + traderLevel) >= 0){
			//System.out.println("hitA: " + value + " totalPoints: " + totalPoints);
			model.setTraderLevel(traderLevel + value);
			model.setTotalPoints(totalPoints - value);
			return traderLevel + value;
		}
		
		return traderLevel;
	}
	
	/**
	 * @param value the level of the player's engineer attribute
	 * @return engineerLevel the player's choice for engineer level
	 */
	public int changeEngineerLevel(int value){
		final int engineerLevel = model.getEngineerLevel();
		final int totalPoints = model.getTotalPoints();
		
		if(value <= totalPoints && (value + engineerLevel) >= 0){
			//System.out.println("hitA: " + value + " totalPoints: " + totalPoints);
			model.setEngineerLevel(engineerLevel + value);
			model.setTotalPoints(totalPoints - value);
			return engineerLevel + value;
		}
		
		return engineerLevel;
	}
	
	/**
	 * @param value the level of the player's fighter attribute
	 * @return fighterLevel the player's choice for fighter level
	 */
	public int changeFighterLevel(int value){
		final int fighterLevel = model.getFighterLevel();
		final int totalPoints = model.getTotalPoints();
		
		if(value <= totalPoints && (value + fighterLevel) >= 0){
			//System.out.println("hitA: " + value + " totalPoints: " + totalPoints);
			model.setFighterLevel(fighterLevel + value);
			model.setTotalPoints(totalPoints - value);
			return fighterLevel + value;
		}
		
		return fighterLevel;
	}
	
	/**
	 * Resets the level of the player's character's attributes
	 */
	public void reset(){
		model.setPilotLevel(0);
		model.setEngineerLevel(0);
		model.setTraderLevel(0);
		model.setFighterLevel(0);
		model.setTotalPoints(16);
	}
	
	/**
	 * Sets the different levels of the player's attributes; also the player's 
	 * name, ship, and credits. Also gets the current instance of the GUI and
	 * sets the content pane to the main page.
	 */
	public void confirm(){
		final Player player = Player.getInstance();
		
		player.setFighterLevel(model.getFighterLevel());
		player.setEngineerLevel(model.getEngineerLevel());
		player.setTraderLevel(model.getTraderLevel());
		player.setPilotLevel(model.getPilotLevel());
		player.setName(name);
		player.setShip(new Gnat());
		player.setCredits(1000);
		
		final GUI frame = GUI.getInstance();
		frame.setContentPane(new MainPagePresenter().getView(), 500, 430);
		
//		System.out.println(player.getName() + " is currently on the " + player.getShip().getName() + " with " + player.getCredits() +
//								" credits and has the following skills:\nPilot: " + player.getPilotLevel()
//								+ "\nFighter: " + Player.getInstance().getFighterLevel() + "\nTrader: " +
//								player.getTraderLevel() + "\nEngineer: " + player.getEngineerLevel());
//		
	}
	
	/**
	 * @return
	 */
	public int getTotalAvailable(){
		return model.getTotalPoints();
	}
	
	/**
	 * @return
	 */
	public int getUsedPoints(){
		return 16 - getTotalAvailable();
	}
	
	/**
	 * @param name
	 */
	public void setName(String name){
		this.name = name;
	}
	
	/**
	 * @return
	 */
	public ConfigScreenView getView(){
		return new ConfigScreenView();
	}
	
	public String toString(){
		return super.toString();
	}
}
